package com.situ.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.situ.bean.Staff;

public interface StaffMapper {
	@Delete({ "delete from staff", "where id = #{id,jdbcType=INTEGER}" })
	int deleteByPrimaryKey(Integer id);

	@Insert({ "insert into staff ( name, ", "phone, dept_id, leader, ", "salary)",
			"values ( #{name,jdbcType=VARCHAR}, ",
			"#{phone,jdbcType=VARCHAR}, #{deptId,jdbcType=INTEGER}, #{leaderId,jdbcType=INTEGER}, ",
			"#{salary,jdbcType=DECIMAL})" })
	@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
	int insert(Staff record);

	@Select({ "select", "id, name, phone, dept_id, leader, salary", "from staff", "where id = #{id,jdbcType=INTEGER}" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
			@Result(column = "dept_id", property = "deptId", jdbcType = JdbcType.INTEGER),
			@Result(column = "leader", property = "leaderId", jdbcType = JdbcType.INTEGER),
			@Result(column = "salary", property = "salary", jdbcType = JdbcType.DECIMAL) })
	Staff selectByPrimaryKey(Integer id);

//select s1.*,dept.`name` as dept_name,s2.`name` as leader_name from staff s1 INNER JOIN dept on s1.dept_id=dept.id left join staff s2 on s1.leader=s2.id
	/*
	 * 1在实体类中需要以个关联的属性 2配置一对一映射
	 * 
	 */

	@Select({ "select id,name,phone,dept_id,leader,salary from staff" })
	@Results(id = "staff", value = { @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
			@Result(column = "dept_id", property = "deptId", jdbcType = JdbcType.INTEGER),
			@Result(column = "leader", property = "leaderId", jdbcType = JdbcType.INTEGER),
			@Result(column = "salary", property = "salary", jdbcType = JdbcType.DECIMAL),
			@Result(column = "dept_id", property = "dept", one = @One(select = "com.situ.mapper.DeptMapper.selectByPrimaryKey")),
			@Result(column = "leader", property = "leader", javaType = Staff.class, one = @One(select = "selectByPrimaryKey")) })
	List<Staff> selectAll();

	@Update({ "update staff", "set name = #{name,jdbcType=VARCHAR},", "phone = #{phone,jdbcType=VARCHAR},",
			"dept_id = #{deptId,jdbcType=INTEGER},", "leader = #{leaderId,jdbcType=INTEGER},",
			"salary = #{salary,jdbcType=DECIMAL}", "where id = #{id,jdbcType=INTEGER}" })
	int updateByPrimaryKey(Staff record);

	// 根据条件来动态查询
	// 在注解中使用动态SQL 必须给语句加上 <scrip></scrip>标签
	@Select({ "<script>", "select * from staff", "<where>", "<if test='deptId !=null'> dept_id = #{deptId}</if>",
			"</where>", "</script>" })
	@ResultMap("staff") // 引用前面id是staff的
	List<Staff> select(Staff staff);
}